package com.edaoe.common;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.edaoe.interceptor.GlobalInterceptor;
import com.edaoe.model._MappingKit;
import com.edaoe.module.log.LogController;
import com.edaoe.module.resource.ResourceController;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.template.Engine;

import java.io.File;
import java.sql.Connection;

/**
 * Created by Dull on 2016/12/28 0028.
 */
public class Config extends JFinalConfig {
    private static Prop p = loadConfig();
    private WallFilter wallFilter;

    /**
     * 启动入口
     */
    public static void main(String[] args) {
        JFinal.start("src/main/webapp", 80, "/");
    }

    private static Prop loadConfig() {
        try {
            // 优先加载生产环境配置文件
            return PropKit.use(new File("/data/projectData/edaoe/config/config.properties"));
        } catch (Exception e) {
            // 找不到生产环境配置文件，再去找开发环境配置文件
            return PropKit.use("config.properties");
        }
    }

    /**
     * 配置常量
     */
    public void configConstant(Constants me) {
        me.setBaseUploadPath(p.get("baseUploadPath"));
        me.setDevMode(p.getBoolean("devMode", false));
    }

    /**
     * 配置路由
     */
    public void configRoute(Routes me) {
        me.add(new FrontRoutes());
        me.add(new AdminRoutes());
        me.add(new ApiRoutes());
        me.add("/log", LogController.class);
        me.add("/resource", ResourceController.class);
    }

    public void configEngine(Engine me) {
        me.addSharedFunction("/_view/common/_layout.html");
        me.addSharedFunction("/_view/common/_header.html");
        me.setDatePattern("yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 抽取成独立的方法，例于 _Generator 中重用该方法，减少代码冗余
     */
    public static DruidPlugin getDruidPlugin() {
        return new DruidPlugin(p.get("jdbcUrl"), p.get("jdbcUsername"), p.get("jdbcPassword").trim());
    }

    /**
     * 配置插件
     */
    public void configPlugin(Plugins me) {
        DruidPlugin druidPlugin = getDruidPlugin();
        wallFilter = new WallFilter();// 加强数据库安全
        wallFilter.setDbType("mysql");
        druidPlugin.addFilter(wallFilter);
        druidPlugin.addFilter(new StatFilter());// 添加 StatFilter 才会有统计数据
        me.add(druidPlugin);

        // 配置ActiveRecord
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.setTransactionLevel(Connection.TRANSACTION_READ_COMMITTED);
        _MappingKit.mapping(arp);// 所有配置在 MappingKit 中搞定
        arp.setBaseSqlTemplatePath(PathKit.getRootClassPath() + "/sql");
        arp.addSqlTemplate("all.sql");
        me.add(arp);

        // cron4j 任务调度
        //me.add(new Cron4jPlugin(p));
    }

    /**
     * 配置全局拦截器
     */
    public void configInterceptor(Interceptors me) {
        me.add(new GlobalInterceptor());
    }

    /**
     * 配置处理器
     */
    public void configHandler(Handlers me) {
        me.add(new DruidStatViewHandler("/druid"));
    }

}
